﻿using Furion;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;
using Tea;

namespace GWB.Core.Common
{
    public static class DingDingHelper
    {
        public static string AppKey = App.Configuration["DingDing:AppKey"];
        public static string AppSecret = App.Configuration["DingDing:AppSecret"];



        /// <summary>
        /// 获取 Token
        /// </summary>
        /// <returns></returns>
        private static string GetAccessToken()
        {
           var access_token=RedisHelper.Read("dingding:access_token");
           if (string.IsNullOrEmpty(access_token))
           {
               access_token = GetToken();
                GetToken();
           }
           return access_token;
        }

        /// <summary>
        /// 获取token
        /// </summary>
        /// <returns></returns>
        public static string GetToken()
        {
            var url = $"https://oapi.dingtalk.com/gettoken?" + $"appkey={AppKey}&appsecret={AppSecret}";
            var result = HttpHelper.HttpGet(url,null, new Dictionary<string, string>(),"");

            var data=JsonConvert.DeserializeObject<ResultEntity>(result);
            RedisHelper.Write("dingding:access_token", data.access_token, new TimeSpan(7100), 0);
            return data.access_token;
        }

        /// <summary>
        /// 获取管理员信息
        /// </summary>
        /// <returns></returns>
        public static DDResultEntity listadmin()
        {
            var url = $"https://oapi.dingtalk.com/topapi/user/listadmin?access_token={GetAccessToken()}";
            var result = HttpHelper.PostData(url, null, Encoding.UTF8, "application/json",
                new Dictionary<string, string>());
            var data = JsonConvert.DeserializeObject<DDResultEntity>(result);
            return data;
        }

        /// <summary>
        /// 内部群发送消息
        /// </summary>
        public static ResultEntity? ChatSend(Markdown markdown)
        {
            var json = new SendEntity()
            {
                chatid = App.Configuration["DingDing:Chatid"],
                msg = new Msg()
                {
                    msgtype = "markdown",
                    markdown = markdown
                }
            };
             Dictionary<string, string> dic = new Dictionary<string, string>();
             var  access_token = GetToken();
            var data = JsonConvert.SerializeObject(json);
           var res=  HttpHelper.PostData("https://oapi.dingtalk.com/chat/send?access_token="+ access_token, data, Encoding.UTF8, "application/json", dic);

           return  JsonConvert.DeserializeObject<ResultEntity>(res);
        }



        public class ResultEntity
        {
            public string access_token { get; set; }
            public int errcode { get; set; }

            public string errmsg { get; set; }

            public string messageId { get; set; }
        }



        public class SendEntity
        {
            public string chatid { get; set; }

            public Msg msg { get; set; }
        }

        public class Msg
        {
            public string msgtype { get; set; }

            public Markdown markdown { get; set; }
        }
        public class Markdown
        {
            public string title { get; set; }

            public string text { get; set; }
        }
       


        public class DDResultEntity
        {
            public int errcode { get; set; }

            public string access_token { get; set; }

            public List<DDResultItem> result { get; set; }

            public string errmsg { get; set; }
            public int expires_in { get; set; }

            public string request_id { get; set; }
        }
        public class DDResultItem
        {
            public int sys_level { get; set; }

            public string userid { get; set; }
        }
    }
}
